<?php
  header('Content-type: application/json');

  $params = array();
  foreach (array('bbox','clientKey') as $p) {
    $params[$p] = $_REQUEST[$p];
  }
  $windsCurrents = explode(',',$_REQUEST['windsCurrents']);

  $data = array();
  for ($i = 0; $i < count($windsCurrents); $i++) {
    $xml = @simplexml_load_file(
       'http://services.asascience.com/ModelService/GetEDSSources.aspx'
      .'?SubCode='.$params['clientKey']
      .'&DataType='.$windsCurrents[$i]
      .'&bbox='.$params['bbox']
    );

    foreach ($xml as $k) {
      array_push($data,array( 
         'id'          => sprintf("%s",$k->attributes()->{'id'})
        ,'description' => sprintf("%s",$k->attributes()->{'description'})
        ,'wmsLayer'    => sprintf("%s",$k->attributes()->{'wmslayeridstr'})
        ,'type'        => $windsCurrents[$i]
      ));
    }
  }

  $description = array();
  foreach ($data as $k => $v) {
    $description[$k] = $v['description'];
  }
  array_multisort($description,SORT_ASC,$data);

  echo(json_encode(array('data' => $data)));
?>
